package com.dxysun.design.composite.safe;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component
{
	private String name;
	private Integer level;

	private List<Component> componentList = new ArrayList<>();

	public Composite(String name, Integer level)
	{
		this.name = name;
		this.level = level;
	}

	@Override
	public void operation()
	{
		System.out.println(name + " 树枝子节点操作 下属节点个数:" + componentList.size());

		for (Component component : componentList)
		{
			//控制显示格式
			if (this.level != null)
			{
				for (int i = 0; i < this.level; i++)
				{
					//打印空格控制格式
					System.out.print(" ");
				}
				for (int i = 0; i < this.level; i++)
				{
					//每一行开始打印一个+号
					if (i == 0)
					{
						System.out.print("+");
					}
					System.out.print("-");
				} //打印标题
				component.operation();
			}
		}
	}

	public boolean addChild(Component component)
	{
		return componentList.add(component);
	}

	public boolean removeChild(Component component)
	{
		return componentList.remove(component);
	}

	public Component getChild(int i)
	{
		return componentList.get(i);
	}
}
